/**
 * 
 */
package cn.biplam.front.template.main;

import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.UserInfo;
import cn.biplam.front.html.HtmlDiv;
import cn.biplam.front.html.HtmlImage;
import cn.biplam.front.html.HtmlPage;
import cn.biplam.front.html.HtmlText;
import cn.biplam.front.html.HtmlValue;
import cn.biplam.front.html.base.Html;
import cn.biplam.front.html.base.HtmlWindow;
import cn.biplam.front.html.base.Const.Align_Type;
import cn.biplam.front.html.base.Const.Cursor_Type;
import cn.biplam.front.html.base.Const.Float_Type;
import cn.biplam.front.html.base.Const.Length_Unit;
import cn.biplam.front.html.base.Const.Overflow_Mode;
import cn.biplam.front.html.base.Const.Position_Mode;
import cn.biplam.front.html.base.Const.Repeat_Type;
import cn.biplam.front.html.base.Const.Visibility_Mode;
import cn.biplam.front.html.base.Const.Window_Event;
import cn.biplam.front.template.base.ButtonImageLittle;
import cn.biplam.front.template.main.page.PagePop;

/**
 * Html主界面模板
 * @author 	WangSong
 * @create 	2009-4-6 上午09:15:49
 * @version 1.0
 */
public class PageMain {

	private HtmlDiv buttons;
	private HtmlDiv domain;
	/**主界面菜单区域 */
	private HtmlDiv menu;
	private HtmlDiv message;
	private HtmlPage page;
	private HtmlDiv prompt_uesr;
	private HtmlDiv promptline;
	private String sHomeUrl="";
	private UserInfo mUser;
	/**用户ID存储对象 */
	private HtmlValue userID;

	/**
	 * 构造方法
	 * @param sTitle	页面标题
	 * @param mUser		用户信息
	 * @param home		网站主目录
	 * @throws ErrorMessage
	 */
	public PageMain(String sTitle,String home,UserInfo mUser) throws ErrorMessage{
		this.sHomeUrl=home;
		this.mUser=mUser;
		page=new HtmlPage(sTitle);
		
		/**放入用户id*/
		this.userID=new HtmlValue("userid",".uid","");
		page.put(this.userID);
		
		/**设置页面页面基本信息*/
		this._setPage(sTitle);
		
		/**放入Banner*/
		page.put(this._createBanner());
		
		/**放入导航区域*/
		page.put(this._createNavigator());
		
		/**放入主功能区域*/
		page.put(this._createDomain());
		
		/**放入系统信息确认框容器*/
		PagePop.setPop(page, this.mUser, sHomeUrl);
		
		/** 放入处理等待层*/
		HtmlWindow wait = new HtmlDiv("syswait");
		wait.setStyleHeight("100",Length_Unit.percent);
		wait.setStyleWidth("100",Length_Unit.percent);
		wait.setVisibility(Visibility_Mode.Hidden);
		wait.setCursor(Cursor_Type.Wait);
		wait.setPositionMode(Position_Mode.Absolute);
		wait.setTop("0");
		wait.setLeft("0");
		wait.setZIndex("99");
		wait.setBackGroundImage(this.sHomeUrl+"/images/empty.gif");
		page.put(wait);
		
		/**放入动态绑定初始化脚本*/
		this._bind("rt.bind();");
		this._bind("document.body.style.visibility='visible';");
//		page.bind("window.onresize=mainResize;");
		//关闭自动调整页面大小,避免一系列的布局递归
	}
	
	/**
	 * 向页面绑定容器放入脚本
	 * @param sScript
	 * @throws ErrorMessage
	 */
	private void _bind(String sScript) throws ErrorMessage{
		this.page.bind(sScript);
	}
	
	/**
	 * 创建页面Banner
	 */
	private Html _createBanner() throws ErrorMessage{
		HtmlDiv banner=new HtmlDiv("banner");
		banner.setStyleHeight("50");
		
		/**创建按钮组容器(页面顶部，可收起)*/
		this.buttons=new HtmlDiv("buttons");
		/**设置buttons*/
		buttons.setStyleHeight("30");
		buttons.setBackGroundImage(sHomeUrl+"/images/banner_bg.gif");
		buttons.setBackGroundRepeat(Repeat_Type.Repeat);
		buttons.setStyleWidth("100",Length_Unit.percent);
		buttons.setPositionMode(Position_Mode.Absolute);
		banner.put(buttons);
		
		/**创建Logo*/
		HtmlImage logo=new HtmlImage(sHomeUrl+"/images/logo.gif");
		/**设置logo图片*/
		logo.setID("logo");
		logo.setMarginLeft("5");
		logo.setTop("10");
		logo.setPositionMode(Position_Mode.Absolute);
		logo.setStyle("z-index", "1");
		logo.setSize("140", "30");
		logo.setCursor(Cursor_Type.Hand);
		logo.bind(Window_Event.OnClick, "bannerResize();");
		banner.put(logo);

		/**设置logosmall图片*/
		HtmlImage logoSmall=new HtmlImage(sHomeUrl+"/images/logo_small.gif");
		logoSmall.setID("logoSmall");
		logoSmall.setMarginLeft("5");
		logoSmall.setTop("1");
		logoSmall.setSize("140", "17");
		logoSmall.setCursor(Cursor_Type.Hand);
		logoSmall.setPositionMode(Position_Mode.Absolute);
		logoSmall.setVisibility(Visibility_Mode.Hidden);
		logoSmall.setStyle("z-index", "1");
		logoSmall.bind(Window_Event.OnClick, "bannerResize();");
		banner.put(logoSmall);
		
		/**设置系统Logo*/
		HtmlImage logoSys=new HtmlImage(sHomeUrl+"/images/logo_sys.gif");
		/**设置logo图片*/
		logoSys.setID("logoSys");
		logoSys.setMarginLeft("155");
		logoSys.setTop("5");
		logoSys.setPositionMode(Position_Mode.Absolute);
		logoSys.setStyle("z-index", "1");
		logoSys.setHeight("20");
		banner.put(logoSys);
		
		
		/**设置msgpre背景*/
		HtmlDiv msgpre=new HtmlDiv("msgpre");//Message前的区域
		msgpre.setStyleHeight("19");
		msgpre.setStyleWidth("175");
		msgpre.setTop("30");
		msgpre.setCursor(Cursor_Type.Hand);
		msgpre.setBackGroundImage(sHomeUrl+"/images/logo_down.gif");
		msgpre.setBackGroundRepeat(Repeat_Type.No_Repeat);
		msgpre.setPositionMode(Position_Mode.Absolute);
		banner.put(msgpre);

		/**设置提示信息行*/
		this.message = new HtmlDiv("message");
		banner.put(message);
		message.setStyleHeight("19");
		message.setBackGroundImage(sHomeUrl+"/images/msgbg.gif");
		message.setBackGroundRepeat(Repeat_Type.Repeat_X);
		message.setPositionMode(Position_Mode.Absolute);
		message.setMarginLeft("175");
		message.setTop("30");
		message.put(this._createPromptLine());//放入提示行内部布局
		
		return banner;
	}

	public Html _createDomain() throws ErrorMessage{
		this.domain=new HtmlDiv("domain");
		return domain;
	}

	/**
	 * 创建导航栏
	 */
	private HtmlDiv _createNavigator() throws ErrorMessage{
		String naviWidth="150";
		
		HtmlDiv navigator = new HtmlDiv("navigator","nd");
		navigator.setFloat(Float_Type.Left);
		navigator.setStyleHeight("100",Length_Unit.percent);
		navigator.setStyleWidth(naviWidth);
		
		/**创建导航标题*/
		HtmlDiv navigator_title=new HtmlDiv("navigator_title","nt");
		navigator.put(navigator_title);
		navigator_title.setStyleHeight("24");
		navigator_title.setStyleWidth(naviWidth);
		navigator_title.setBackGroundRepeat(Repeat_Type.Repeat_X);
		navigator_title.setBackGroundImage(this.sHomeUrl+"/images/title_bg.gif");
		//设置文字
		HtmlText text=new HtmlText(null,"nst","主菜单");
		navigator_title.put(text);
		text.setFloat(Float_Type.Left);
		text.setMarginLeft("5");
		//设置图片
		HtmlWindow button = new ButtonImageLittle("navigator-shrink",sHomeUrl+"/images/retracting.gif").get();
		navigator_title.put(button);
		button.setFloat(Float_Type.Right);
		button.setTitle("折叠主菜单");
		button.bind(Window_Event.OnClick, "navigtor_shrink();");
		
		/**创建导航标题2*/
		HtmlDiv navigator_title2=new HtmlDiv("navigator_title2","nt2");
		navigator.put(navigator_title2);
		navigator_title2.setAlign(Align_Type.center);
		navigator_title2.setBackGroundImage(this.sHomeUrl+"/images/title_bg_v.gif");
		navigator_title2.setBackGroundRepeat(Repeat_Type.Repeat_Y);
		navigator_title2.setCursor(Cursor_Type.Default);
		navigator_title2.setPositionMode(Position_Mode.Absolute);
		navigator_title2.setVisibility(Visibility_Mode.Hidden);
		navigator_title2.setStyleHeight(naviWidth);
		navigator_title2.setStyleWidth("25");
		navigator_title2.setTop("0");
		//设置图片
		HtmlWindow button2 = new ButtonImageLittle("navigator-expand",sHomeUrl+"/images/spread.gif").get();
		button2.setMarginRight("3");
		navigator_title2.put(button2);
		button2.setTitle("展开主菜单");
		button2.bind(Window_Event.OnClick, "navigator_expand();");
		//设置文字
		HtmlText text2=new HtmlText(null,"nst","主<BR>菜<BR>单");
		navigator_title2.put(text2);
		text2.setFontSize("17");
		text2.setLineHeight("55");
		
		/**创建菜单区域*/
		this.menu = new HtmlDiv("menu");
		navigator.put(menu);
		menu.setOverflow(Overflow_Mode.auto);
		menu.setStyleWidth(naviWidth);
		
		this._bind("mainResize();");

		return navigator;
	}
	
	/**
	 * 生成提示行布局
	 * @throws ErrorMessage
	 */
	private HtmlWindow _createPromptLine() throws ErrorMessage {
		HtmlWindow line=new HtmlDiv();
		this.prompt_uesr=new HtmlDiv("prompt_uesr");
		this.prompt_uesr.setFloat(Float_Type.Left);
		this.prompt_uesr.setMarginTop("3");
		this.promptline=new HtmlDiv("promptline");
		this.promptline.setFloat(Float_Type.Left);
		this.promptline.setOverflow(Overflow_Mode.hidden);
		this.promptline.setStyleHeight("14");
		this.promptline.setMarginTop("3");
		line.put(prompt_uesr);
		line.put(promptline);
		return line;
	}

	/**
	 * 设置页面Title meta 导入文件、Body基本信息
	 */
	private void _setPage(String sTitle) throws ErrorMessage{
		new MainImport().get(page, sHomeUrl);
//		page.setStyle("visibility", "hidden");
	}

	/**
	 * 获取对象及其内容的 HTML 形式
	 * @return
	 * @throws ErrorMessage
	 */
	public HtmlPage get() throws ErrorMessage{
		return this.page;
	}
	
	/**
	 * 放入Banner按钮组
	 * @param html
	 * @throws ErrorMessage
	 */
	public void putButtons(Html html) throws ErrorMessage{
		/**放入按钮组*/
		this.buttons.put(html);
	}
	
	/**
	 * 向主功能区域放入内容
	 * @param html
	 */
	public void putDomain(Html html) throws ErrorMessage{
		this.domain.put(html);
	}
	
	/**
	 * 放入菜单
	 * @param html
	 * @throws ErrorMessage
	 */
	public void putMenu(Html html) throws ErrorMessage{
		this.menu.put(html);
	}
	
	/**
	 * 向信息提示行放入提示消息
	 * @param html
	 * @throws ErrorMessage
	 */
	public void putMessage(Html html) throws ErrorMessage{
		this.promptline.put(html);
	}
	
	/**
	 * 放入用户称呼
	 * @param userCall
	 * @throws ErrorMessage 
	 */
	public void putUserCall(Html html) throws ErrorMessage{
		this.prompt_uesr.put(html);
	}

	public void setUserID(String  userID) {
		this.userID.replace(userID);
	}
}
